package com.example.tests;

import com.example.utils.SortedListOf;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

public class GroupCreationTestSet extends TestBase{

    @BeforeClass
    public void prepare(){
        applicationManager.navigateTo()
                .mainPage()
                .groupsList();
    }

    @Test(dataProvider = "groupsFromXMLFile")
    public void testValidDataGroupCreation(GroupData groupData) throws Exception {

        //save old state
        //SortedListOf<GroupData> oldGroupList = applicationManager.getGroupHelper().getGroupList();
        SortedListOf<GroupData> oldGroupList
                = new SortedListOf<GroupData>(applicationManager.getModel().getGroupList());

        //actions
        applicationManager.getGroupHelper().createGroup(groupData);

        //save new state
        SortedListOf<GroupData> newGroupList = applicationManager.getModel().getGroupList();

        //compare states
        assertThat(newGroupList, equalTo(oldGroupList.withAdded(groupData)));

        //compare data with model
        if (shouldCheck()){
            if("yes".equals(applicationManager
                    .getProperty("check.db"))){
                    assertThat(applicationManager.getModel().getGroupList()
                            , equalTo(applicationManager.getHibernateHelper().listGroups()));
            }

            if("yes".equals(applicationManager
                    .getProperty("check.ui"))){
                assertThat(applicationManager.getModel().getGroupList()
                        , equalTo(applicationManager.getGroupHelper().getUiGroupList()));
            }
        }
    }


}
